<?php
// $Id$

/**
 * Controller_Admin_sms 控制器
 */
class Controller_Admin_Sms extends Controller_Abstract
{
	function _format_appendSql()
	{
		if (isset($_SESSION['acct']))
		{
			if ($_SESSION['acct']['admin_id'] == -1 && $_SESSION['acct']['agent_id'] == -1)
			{
				//为管理
				//$append['']
				$append['admin_id'] = $_SESSION['acct']['id'];
	
			}else
			{
				$append['admin_id'] = $_SESSION['acct']['admin_id'];
				$append['id'] = Acct::_get_have_acctid($_SESSION['acct']['id'], $_SESSION['acct']['admin_id']);
	
			}
		}
		return $append;
	}
	function actionadminsms()
	{
	    $ids = $this->_context->ids;
		//$param = $this->_format_appendSql();
		if (!is_array($ids) && $ids)$ids = array($ids);
		$adminSmsbalance = Acct::getACCadminSmsBalance();
		//if($_SESSION['acct']['acclevel'] != 0)return $this->success('联系管理修改');
		foreach ($ids as $k=>$v)
		{
			$Par = acct::find('id = ?',$v)->setColumns('id,adminsms,admin_id')->getOne();
			//$personIDS['id'] = Acct::_get_have_acctidTrue($v, $Par->admin_id);
			$Par->adminsms = $Par->adminsms ? 0 : 1;
			$Par->save(0,'update');
		}
		return $this->success('操作成功');
	}
	/**
	 * 短信开关
	 */
	function actionsmsflag()
	{
		$ids = $this->_context->ids;
		//$param = $this->_format_appendSql();
		if (!is_array($ids) && $ids)$ids = array($ids);
		$adminSmsbalance = Acct::getACCadminSmsBalance();
		//if($_SESSION['acct']['acclevel'] != 0)return $this->success('联系管理修改');
		foreach ($ids as $k=>$v)
		{
			$Par = acct::find('id = ?',$v)->setColumns('id,smsflag,admin_id')->getOne();
			$personIDS['id'] = Acct::_get_have_acctidTrue($v, $Par->admin_id);
			
			if (count($personIDS['id']))
			{
				$person = Acct::find($personIDS)->getAll();
				foreach($person as $i=>$ii)
				{
					$ii->smsflag = $Par->smsflag ? 0 : 1;
					$ii->smsbalance = $adminSmsbalance; #等于管理设置
					$ii->save(0,'update');
				}
			}
			$Par->smsflag = $Par->smsflag ? 0 : 1;
			$Par->save(0,'update');
		}
		return $this->success('操作成功');
	}
	function actionapply()
	{
		$this->union_table = acct::meta();
		if ($this->_context->agent_id)
		{
			$has_subid = Acct::_get_have_acctid($_SESSION['acct']['id'], $_SESSION['acct']['admin_id']);
				
			if (in_array($this->_context->agent_id,$has_subid))
			{
				$this->lists(array('id'=>$has_subid,'admin_id'=>intval($this->_context->admin_id),'agent_id'=>intval($this->_context->agent_id)));
			}else
			{
				exit('权限不够');
			}
		}else
		{
			if ($this->_context->acctname)
			{
				$this->lists(parent::_format_appendSql());//array('admin_id'=>-1)为管理
			}else {
				$has_subid['id'] = Acct::_get_have_acctid($_SESSION['acct']['id'], $_SESSION['acct']['admin_id']);
				$acct = Acct::find('acctname = ?',$_SESSION['acct']['acctname'])->getOne();
				if($acct->is_Onelevel)
				{
					//$has_subid['id'][] = $_SESSION['acct']['id'];
				}
				if(count($has_subid['id']) == 0)$has_subid['id'] = array(-1);
				$this->lists($has_subid);
			}
		}
	}
	function actionadd()
	{
		$adminbalance = Acct::getACCadminSmsBalance();
		
		$this->union_table = Acct::meta();
		$handle = new helper_autoinsert($this->union_table);
		$form = new Form_sms_add(url('sms/add'));
		
		$result = $handle->add($form, 'id', $_GET);
		if ($this->_context->isGET())
		{
			$this->_view['form'] = $result;
			$this->_viewname = '../form/add';
		}else {
		        $smsmsg = trim($this->_context->smsmsg);
		        $smstype = intval($this->_context->smstype);
				if(mb_strlen($smsmsg,'utf-8')>=20 && $smstype == 0)
				{
					return $this->error('内容长度必须低于20字符');
				}
				$smssign = trim($this->_context->smssign);
				$smssign = str_replace('【', '', $smssign);
				$smssign = str_replace('】', '', $smssign);
		        if(mb_strlen($smssign,'utf-8')>=10)
				{
					return $this->error('签名长度必须低于10字符');
				}
				if (!$smsmsg)
				{
					return $this->error('内容必须填写');
				}else{
					$acct = acct::find('id = ?',intval($this->_context->id))->getOne();
					if($acct->adminsms == 0 && $_SESSION['acct']['acclevel'] <> 0)
					{
						return $this->error('对不起,当前是管理锁定,请联系管理修改内容');
						die;
					}
					if($acct->id())
					{
						$acct->smsmsg = $smsmsg;
						$acct->smssign = '【'.$smssign.'】';
						$acct->smstype = $smstype;
						$acct->smsflag = 0;
						$acct->save(0,'update');
						//$_SESSION['acct']['smsflag'] = 0;
						//$_SESSION['acct']['smsmsg'] = $smsmsg;
						//$_SESSION['acct']['smssign'] = $acct->smssign;
						
						return $this->success('内容修改成功,请等待管理审核内容!');
					}else{
						return $this->error('您当前的短信开关未打开,请联系上级!');
					}
				}
			QDB::getConn()->execute('UPDATE acct set smsbalance = '.$adminbalance.' where id = '.intval($this->_context->id));
			Syslog::logs('修改代理:'.$_POST['id'].'的短信内容');
			 
			//return call_user_func_array(array($this,$handle->status),array($result));
		}	
	}
	/**
	 * 日志
	 */
	function actionlog()
	{
		if ($_SESSION['acct']['admin_id'] == -1 && $_SESSION['acct']['agent_id'] == -1)
		{
			//为管理
			//$append['']
			$append['admin_id'] = $_SESSION['acct']['id'];
		
		}else
		{
			$append['admin_id'] = $_SESSION['acct']['admin_id'];
			$append['agent_id'] = Acct::_get_have_acctid($_SESSION['acct']['id'], $_SESSION['acct']['admin_id']);
			if (count($append['agent_id']) == 0)
			{
				$append['agent_id'] = array($_SESSION['acct']['id']); 
			}
		}
		$this->union_table = Smslog::meta();
		$this->lists($append);
// 		$this->union_table = Smslog::meta();
// 		$this->lists($append);
	}
}